package org.jflame.commons.excel.handler;

import org.jflame.commons.excel.ExcelColumnProperty;
import org.jflame.commons.model.Chars;
import org.jflame.commons.util.StringHelper;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;

public interface IExcelRowWriter<T> {

    /**
     * 对象数据对填充excel行.
     * 
     * @param rowData 对象数据
     * @param excelRow excel行
     * @see org.apache.poi.ss.usermodel.Row
     */
    public default void fillRow(T rowData, Row excelRow) {
        fillRow(rowData, excelRow, null);
    }

    public default void fillRow(T rowData, Row excelRow, CellStyle defaultCellStyle) {
        fillRow(rowData, excelRow, 0, null);
    }

    /**
     * 对象数据对填充excel行.
     * 
     * @param rowData 对象数据
     * @param excelRow excel行
     * @param startColIndex 开始填充的列索引
     * @param defaultCellStyle 单元格默认样式
     * @see org.apache.poi.ss.usermodel.Row
     */
    public void fillRow(T rowData, Row excelRow, int startColIndex, CellStyle defaultCellStyle);

    /**
     * 设置单元格值,值类型与CellType有的类型匹配则设置,否则不设置
     * 
     * @param cell
     * @param rawValue
     * @param colProp
     * @return 成功赋值返回true
     */
    default boolean setCellRawValue(Cell cell, Object rawValue, ExcelColumnProperty colProp) {
        if (rawValue instanceof String) {
            if (colProp.getColumnType() == 0 || colProp.getColumnType() == 1) {
                String strValue = (String) rawValue;
                // 简单支持下常见的换行富文本
                if (StringHelper.containsAny(strValue, Chars.CR, Chars.LF)) {
                    cell.setCellValue(cell.getSheet()
                            .getWorkbook()
                            .getCreationHelper()
                            .createRichTextString(strValue));
                } else {
                    cell.setCellValue(strValue);
                }
            } else if (colProp.getColumnType() == 2) {
                cell.setCellFormula((String) rawValue);
            } else {
                return false;
            }
        } else {
            if (colProp.getColumnType() == 0) {
                // 显示格式由单元格样式设定
                if (rawValue instanceof Number) {
                    cell.setCellValue(((Number) rawValue).doubleValue());
                } else if (rawValue instanceof Date) {
                    cell.setCellValue((Date) rawValue);
                } else if (rawValue instanceof LocalDateTime) {
                    cell.setCellValue((LocalDateTime) rawValue);
                } else if (rawValue instanceof LocalDate) {
                    cell.setCellValue((LocalDate) rawValue);
                } else if (rawValue instanceof Calendar) {
                    cell.setCellValue((Calendar) rawValue);
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        return true;
    }
}
